package com.solomon.travel.common.web.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.solomon.travel.common.base.controller.BaseController;

@Controller
@RequestMapping("/web")
public class CkeditorController extends BaseController {
	
	@RequestMapping("/editorupload")
	public void ckeditorUpload(HttpServletRequest request, HttpServletResponse response,@RequestParam MultipartFile upload) throws Exception{
		if (!upload.isEmpty()) {
			String callback =request.getParameter("CKEditorFuncNum");
		    String contentType = upload.getContentType();
		    PrintWriter out = response.getWriter();
		    if (contentType.equals("image/jpeg") || contentType.equals("image/pjpeg") || contentType.equals("image/png") || contentType.equals("image/x-png") ||contentType.equals("image/gif") || contentType.equals("image/bmp")){
		    	 String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				 String originalName = upload.getOriginalFilename();
				 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
				 String path = firstKey + "/ckeditor/" + datePath + "/" + fileName;
				 ossClient.putObject(bucketName, path, upload.getInputStream());
				 out.println("<script type=\"text/javascript\">");    
			     out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + ossurl+path+ "','')");    
			     out.println("</script>");
		    }else{
		    	out.println("<script type=\"text/javascript\">");    
	            out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件格式不正确（必须为.jpg/.gif/.bmp/.png文件）');");   
	            out.println("</script>");
		    }
		}
	}
}
